//
// Created by tongj on 2024/1/13.
//

#ifndef TBS_CPP_RESULT_CUH
#define TBS_CPP_RESULT_CUH

template<typename S, typename E> Result<S, E> Result<S, E>::make_success(S &&val) {
  Result<S, E> r;
  r._s = std::move(val);
  r._success = true;
  return r;
}

template<typename S, typename E> Result<S, E> Result<S, E>::make_error(E &&val) {
  Result<S, E> r;
  r._e = std::move(val);
  r._success = false;
  return r;
}

template<typename S, typename E> bool Result<S, E>::is_success() const {
  return _success;
}

template<typename S, typename E> S Result<S, E>::value() const {
  if (is_success()) {
	return _s;
  } else {
	throw _e;
  }
}

template<typename S, typename E> E Result<S, E>::error() const {
  if (is_success()) {
	throw base_error("has value for result");
  }
  return _e;
}

template<typename EP, typename S, typename E> Result<S, E> make_result(EP exp, S &&v, E &&e) {
  if (exp) {
	return Result<S, E>::make_success(std::forward<S>(v));
  } else {
	return Result<S, E>::make_error(std::forward<E>(e));
  }
}
#endif //TBS_CPP_RESULT_CUH
